<template>
  <div style="padding:0px 8px">
    <el-card class="configContainer" style="height: 100%;">
      <div style="display: flex;flex-direction: row;align-items: center">
        <h4 class="card-title"> 日志管理 </h4>
        <div class="space"/>
      </div>
      <el-divider/>
      <div style="margin: 10px 20px">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item>
            <el-select clearable v-model="searchForm.state" placeholder="请选择正常日志" style="width:100%">
              <el-option label="全部日志"  :key="null" :value="null"/>
              <el-option label="正常日志"  :key="200" :value="200"/>
              <el-option label="异常日志" :key="500" :value="500"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input clearable class="input-150 ml-5" placeholder="请输入请求者" suffix-icon="el-icon-search"
                      v-model="searchForm.userName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-date-picker clearable class="input-150 ml-5" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="开始时间"
                            v-model="searchForm.startTime"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-date-picker clearable class="input-150 ml-5" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="结束时间"
                            v-model="searchForm.endTime"></el-date-picker>
          </el-form-item>
        </el-form>
        <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
        <el-button type="warning" @click="reset">重置</el-button>

        <el-popconfirm
            width="160"
            confirm-button-text='确定'
            cancel-button-text='我再想想'
            icon-color="red"
            title="您确定批量删除这些数据吗？"
            @confirm="delBatch">
          <template #reference>
            <el-button type="danger">
              <el-icon>
                <Delete/>
              </el-icon>
              批量删除
            </el-button>
          </template>
        </el-popconfirm>
      </div>

      <el-table height="400px"  :data="tableData" :header-cell-style="{background:'#f8f8f8',height:'40px',color:'#2b2b2b'}"  style="width: 100%;margin-top: 16px;"
                :cell-style="{'text-align':'center'}"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="40"></el-table-column>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="reqIp" label="客户端ip">
          <template #default="s">
            <el-tooltip class="item" effect="dark" :content="s.row.reqIp" placement="top-start">
              <span class="req-string cursor">
                {{ s.row.reqIp }}
              </span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="reqApi" label="请求api">
          <template #default="s">
            <el-tooltip class="item" effect="dark" :content="s.row.reqApi" placement="top-start">
              <span class="req-string cursor">
                {{ s.row.reqApi }}
              </span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="请求参数">
          <template #default="s">
            <span class="req-string cursor" @click="showJson('请求参数',s.row.reqParame)">
              {{ s.row.reqParame }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="reqToken" label="token">
            <template #default="s">
              <span class="req-string cursor" @click="showJson('令牌',JSON.stringify({token:s.row.reqToken}))">
                {{ s.row.reqToken }}
              </span>
            </template>
        </el-table-column>
        <el-table-column label="请求头">
          <template #default="s">
            <span class="req-string cursor" @click="showJson('请求头',s.row.reqHeader)">
              {{ s.row.reqHeader }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="reqType" label="请求类型">
          <template #default="s">
            <span class="req-type-box">
              <b v-if=" s.row.reqType == 'GET' " style="background-color: #00A65A;">GET</b>
              <b v-else-if=" s.row.reqType == 'POST' " style="background-color: #0073B7;">POST</b>
              <b v-else style="background-color: #FF6A00;">{{ s.row.reqType }}</b>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="返回状态">
          <template #default="s">
            <span>
              <b v-if="s.row.resCode == 200" style="color: green;">{{ s.row.resCode }}</b>
              <b v-else-if="s.row.resCode == 500 || s.row.resCode == 501" style="color: red;">{{ s.row.resCode }}</b>
              <b v-else style="color: blue;">{{ s.row.resCode }}</b>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="返回信息">
          <template #default="s">
            <span class="req-string cursor" @click="showJson('返回信息',s.row.resString)">
              {{ s.row.resString }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="请求人">
          <template #default="s">
            <el-tooltip class="item" effect="dark" :content="s.row.userName" placement="top-start">
              <span class="req-string cursor">
                {{ s.row.userName }}
              </span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="请求开始">
          <template #default="s">
            <el-tooltip class="item" effect="dark" :content="s.row.startTime" placement="top-start">
              <span class="req-string cursor">
                {{ s.row.startTime }}
              </span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="endTime" label="请求结束">
          <template #default="s">
            <el-tooltip class="item" effect="dark" :content="s.row.endTime" placement="top-start">
              <span class="req-string cursor">
                {{ s.row.endTime }}
              </span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="costTime" label="时间/ms"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-popconfirm title="确认删除吗?" @confirm="del(scope.row.id)">
              <template #reference>
                <el-button link type="danger" size="small">
                  <el-icon>
                    <Delete/>
                  </el-icon>
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :page-sizes="[10, 20, 50, 100]"
          :page-count="pagecount"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="currentChange"
          style="margin-top: 16px;width: 100%;display: flex;align-items: center;justify-content: center;"
          background layout="total, sizes, prev, pager, next, jumper"/>
    </el-card>
    <el-dialog v-model="show" :title="title" center>
      <json-viewer
          :expand-depth=5
          copyable
          boxed
          sort="true"
          :value="JsonData">
      </json-viewer>
    </el-dialog>
  </div>
  <CompanyText/>
</template>

<script>
import CompanyText from "../../../components/CompanyText.vue";

export default {
  name: "ApiLog",
  components: {
    CompanyText
  },
  data() {
    return {
      searchForm :{
        userName : null,
        state : null,
        tartTime : null,
        endTime : null,
      },
      JsonData: {},
      show: false,
      state_type: "primary",
      state_msg: 1,
      tableData: [],
      state: null,
      userName: null,
      multipleSelection: [],
      pageNum: 1,
      pagesize: 10,
      total: 0,
      startTime: null,
      endTime: null
    }
  },
  created() {
    this.load()
  },
  methods: {
    showJson(title, JsonData) {
      this.show = true;
      this.JsonData = JSON.parse(JsonData)
      console.info(this.JsonData)
      this.title = title
    },
    load() {
      this.$http.get("/apiLog/getApiLogs", {
        params: this.searchForm
      }).then(res => {
        this.tableData = res.data.records;
        this.pagecount = res.data.pages;
        this.total = res.data.total;
        this.current = res.data.current;
      })
    },
    del(id) {
      let data = new FormData();
      data.append('id', id);
      this.$http.post("/apiLog/deleteById", data).then(res => {
        console.info(res.stateCode);
        if (res.code === 0) {
          this.$message.success(res.userMsg)
          this.load()
        } else {
          this.$message.success(res.userMsg)
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      let data = new FormData();
      data.append('ids', ids);
      this.$http.post("/apiLog/delBatchById", data).then(res => {
        console.info(res)
        if (res.code === 0) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.searchForm = {};
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.searchForm.pageSize = pageSize
      this.load()
    },
    currentChange(current) {
      this.searchForm.pageNum = current;
      console.info(this.current)
      this.load();
    },
  }
}
</script>

<style scoped>

.my-awesome-json-theme {
  background: #fff;
  white-space: nowrap;
  color: #525252;
  font-size: 14px;
  font-family: Consolas, Menlo, Courier, monospace;
}
.jv-ellipsis {
  color: #999;
  background-color: #eee;
  display: inline-block;
  line-height: 0.9;
  font-size: 0.9em;
  padding: 0px 4px 2px 4px;
  border-radius: 3px;
  vertical-align: 2px;
  cursor: pointer;
  user-select: none;
}

.jv-button {
  color: #49b3ff
}


.jv-array {
  color: #111111
}



.jv-boolean {
  color: #fc1e70
}

.jv-function {
  color: #067bca
}

.jv-number {
  color: #fc1e70
}

.jv-number-float {
  color: #fc1e70
}

.jv-number-integer {
  color: #fc1e70
}

.jv-object {
  color: #111111
}

.jv-undefined {
  color: #e08331
}

.jv-string {
  color: #42b983;
  word-break: break-word;
  white-space: normal;
}




:before {
  padding: 0px 2px;
  border-radius: 2px;
}


:before {
  background: #eee;
}


.req-string {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.req-type-box {
  color: #FFF;
}

.req-type-box b {
  padding: 3px 7px;
  border-radius: 3px;
  font-size: 12px;
}
.cursor{
  cursor:pointer;
}
 .jv-key {
  color: tomato;
  margin-right: 4px;
}
.configContainer {
  margin: 10px 8px;
  padding: 0px;
  /* border: red 1px solid; */
}
</style>
